Shader "Hidden/Fullscreen/UpSampleSeparated"
{
    HLSLINCLUDE
    #pragma vertex Vert

    #pragma target 4.5
    #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch

    #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl"

    TEXTURE2D_X(_Source);
    TEXTURE2D_X(_Source1);
    TEXTURE2D_X(_Source2);
    TEXTURE2D_X(_Source3);
    TEXTURE2D_X(_Source4);

    TEXTURE2D_X(_SceneSource1); // 场景Bloom缓冲区1
    TEXTURE2D_X(_SceneSource2); // 场景Bloom缓冲区2
    TEXTURE2D_X(_SceneSource3); // 场景Bloom缓冲区3
    TEXTURE2D_X(_SceneSource4); // 场景Bloom缓冲区4

    float _CharacterIntensity;
    float _SceneIntensity;

    float4 _Offset;
    float _Intensity;
    float4 _UpSize;
    int _BufferCount;
    float _SceneViewMode;

    float4 HoBlur(Varyings varyings) : SV_Target
    {
        UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);

        // 为Scene视图优化UV计算
        float2 uv = varyings.positionCS.xy * _ScreenSize.zw;
        if (_SceneViewMode < 0.5) // Game视图
        {
            uv = uv * _RTHandleScale.xy / _DynamicResolutionFullscreenScale.xy * pow(2, _UpSize.x + 2);
        }
        else // Scene视图
        {
            uv = uv * _RTHandleScale.xy * pow(2, _UpSize.x + 2);
        }

        float4 color = float4(0, 0, 0, 0);

        // 根据_UpSize.x决定采样方式
        if (_UpSize.x <= 2) // 采样4次 (对342、160的图进行一维模糊)
        {
            // downSampleBuffers[1], downSampleBuffers[2]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -1.5, 0), 0) *
                0.18877;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -0.5, 0), 0) *
                0.31123;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 0.5, 0), 0) *
                0.31123;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 1.5, 0), 0) *
                0.18877;
        }
        else if (_UpSize.x == 3) // 采样6次 (对72图进行一维模糊)
        {
            // downSampleBuffers[3]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -2.5, 0), 0) *
                0.09077;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -1.5, 0), 0) *
                0.17215;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -0.5, 0), 0) *
                0.23707;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 0.5, 0), 0) *
                0.23707;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 1.5, 0), 0) *
                0.17215;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 2.5, 0), 0) *
                0.09077;
        }
        else if (_UpSize.x == 4) // 采样14次 (对36图进行一维模糊)
        {
            // downSampleBuffers[4]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -6.5, 0), 0) *
                0.016;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -5.5, 0), 0) *
                0.02893;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -4.5, 0), 0) *
                0.04741;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -3.5, 0), 0) *
                0.07038;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -2.5, 0), 0) *
                0.09465;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -1.5, 0), 0) *
                0.11532;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * -0.5, 0), 0) *
                0.1273;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 0.5, 0), 0) *
                0.1273;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 1.5, 0), 0) *
                0.11532;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 2.5, 0), 0) *
                0.09465;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 3.5, 0), 0) *
                0.07038;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 4.5, 0), 0) *
                0.04741;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 5.5, 0), 0) *
                0.02893;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(_Offset.x * 6.5, 0), 0) *
                0.016;
        }

        return color * _Intensity;
    }

    float4 VerBlur(Varyings varyings) : SV_Target
    {
        UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);

        // 为Scene视图优化UV计算
        float2 uv = varyings.positionCS.xy * _ScreenSize.zw;
        if (_SceneViewMode < 0.5) // Game视图
        {
            uv = uv * pow(2, _UpSize.x + 2);
        }
        else // Scene视图
        {
            uv = uv * _RTHandleScale.xy * pow(2, _UpSize.x + 2);
        }

        float4 color = float4(0, 0, 0, 0);

        // 根据_UpSize.x决定采样方式
        if (_UpSize.x <= 2) // 采样4次 (对342、160的图进行一维模糊)
        {
            // downSampleBuffers[1], downSampleBuffers[2]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -1.5), 0) *
                0.18877;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -0.5), 0) *
                0.31123;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 0.5), 0) *
                0.31123;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 1.5), 0) *
                0.18877;
        }
        else if (_UpSize.x == 3) // 采样6次 (对72图进行一维模糊)
        {
            // downSampleBuffers[3]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -2.5), 0) *
                0.09077;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -1.5), 0) *
                0.17215;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -0.5), 0) *
                0.23707;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 0.5), 0) *
                0.23707;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 1.5), 0) *
                0.17215;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 2.5), 0) *
                0.09077;
        }
        else if (_UpSize.x == 4) // 采样14次 (对36图进行一维模糊)
        {
            // downSampleBuffers[4]
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -6.5), 0) *
                0.016;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -5.5), 0) *
                0.02893;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -4.5), 0) *
                0.04741;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -3.5), 0) *
                0.07038;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -2.5), 0) *
                0.09465;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -1.5), 0) *
                0.11532;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * -0.5), 0) *
                0.1273;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 0.5), 0) *
                0.1273;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 1.5), 0) *
                0.11532;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 2.5), 0) *
                0.09465;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 3.5), 0) *
                0.07038;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 4.5), 0) *
                0.04741;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 5.5), 0) *
                0.02893;
            color += SAMPLE_TEXTURE2D_X_LOD(_Source, s_linear_clamp_sampler, uv + float2(0, _Offset.x * 6.5), 0) *
                0.016;
        }

        return color * _Intensity;
    }


    float4 UpSampleBlend(Varyings varyings) : SV_Target
    {
        UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);

        // 为Scene视图优化UV计算
        float2 uv = varyings.positionCS.xy * _ScreenSize.zw;
        if (_SceneViewMode < 0.5) // Game视图
        {
            uv = uv * _RTHandleScale.xy / _DynamicResolutionFullscreenScale.xy;
        }
        else // Scene视图
        {
            uv = uv * _RTHandleScale.xy;
        }

        // 对多个verSampleBuffer进行采样相加
        float4 color = float4(0, 0, 0, 0);

        // 根据_BufferCount决定要采样多少个纹理
        // if (_BufferCount >= 1)
        //     color += SAMPLE_TEXTURE2D_X_LOD(_Source1, s_linear_clamp_sampler, uv, 0);
        //
        // if (_BufferCount >= 2)
        //     color += SAMPLE_TEXTURE2D_X_LOD(_Source2, s_linear_clamp_sampler, uv, 0);
        //
        // if (_BufferCount >= 3)
        //     color += SAMPLE_TEXTURE2D_X_LOD(_Source3, s_linear_clamp_sampler, uv, 0);
        // if (_BufferCount >= 4)
        //     color += SAMPLE_TEXTURE2D_X_LOD(_Source4, s_linear_clamp_sampler, uv, 0);

        // 采样角色Bloom纹理并应用角色强度
        if (_BufferCount >= 1)
            color += SAMPLE_TEXTURE2D_X_LOD(_Source1, s_linear_clamp_sampler, uv, 0) * _CharacterIntensity;
        if (_BufferCount >= 2)
            color += SAMPLE_TEXTURE2D_X_LOD(_Source2, s_linear_clamp_sampler, uv, 0) * _CharacterIntensity;
        if (_BufferCount >= 3)
            color += SAMPLE_TEXTURE2D_X_LOD(_Source3, s_linear_clamp_sampler, uv, 0) * _CharacterIntensity;
        if (_BufferCount >= 4)
            color += SAMPLE_TEXTURE2D_X_LOD(_Source4, s_linear_clamp_sampler, uv, 0) * _CharacterIntensity;
        // 采样场景Bloom纹理并应用场景强度
        if (_BufferCount >= 1)
            color += SAMPLE_TEXTURE2D_X_LOD(_SceneSource1, s_linear_clamp_sampler, uv, 0) * _SceneIntensity;
        if (_BufferCount >= 2)
            color += SAMPLE_TEXTURE2D_X_LOD(_SceneSource2, s_linear_clamp_sampler, uv, 0) * _SceneIntensity;
        if (_BufferCount >= 3)
            color += SAMPLE_TEXTURE2D_X_LOD(_SceneSource3, s_linear_clamp_sampler, uv, 0) * _SceneIntensity;
        if (_BufferCount >= 4)
            color += SAMPLE_TEXTURE2D_X_LOD(_SceneSource4, s_linear_clamp_sampler, uv, 0) * _SceneIntensity;

        return color * 0.6;
    }
    ENDHLSL

    SubShader
    {
        Pass
        {
            Name "hoBlur"

            ZWrite Off
            ZTest Always
            Blend Off
            Cull Off

            HLSLPROGRAM
            #pragma fragment HoBlur
            ENDHLSL
        }
        Pass
        {
            Name "verBlur"

            ZWrite Off
            ZTest Always
            Blend Off
            Cull Off

            HLSLPROGRAM
            #pragma fragment VerBlur
            ENDHLSL
        }


        Pass
        {
            Name "UpSample Blend"

            ZWrite Off
            ZTest Always
            Blend One One
            Cull Off

            HLSLPROGRAM
            #pragma fragment UpSampleBlend
            ENDHLSL
        }
    }
    Fallback Off
}